package com.lbj.googleplay.googleplay100.ui.holder;

import android.animation.Animator;
import android.animation.ValueAnimator;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.lbj.googleplay.googleplay100.R;
import com.lbj.googleplay.googleplay100.domain.AppInfo;
import com.lbj.googleplay.googleplay100.http.HttpHelper;
import com.lbj.googleplay.googleplay100.utils.UIUtils;

import org.xutils.ImageManager;
import org.xutils.x;

import java.util.ArrayList;

public class DetailSafeHolder extends BaseHolder<AppInfo> {

    private ImageView[] mSafeIcons;//安全标识图片
    private ImageView[] mDesIcons;//安全描述图片
    private TextView[] mSafeDes;//安全描述文字
    private LinearLayout[] mSafeDesBar;//安全描述条目(图片+文字)
    private ImageManager imageManager;
    private RelativeLayout rlDesRoot;
    private LinearLayout llDesRoot;//安全描述布局
    private int mDesHeight; //安全描述布局的高度
    private LinearLayout.LayoutParams layoutParams;//安全描述布局的参数
    private ImageView ivArrow;//箭头图片
    private  boolean isOpen = false;//标记安全描述开关状态,默认关

    @Override
    public View initView() {
        View view = UIUtils.inflate(R.layout.layout_detail_safeinfo);

        mSafeIcons = new ImageView[4];
        mSafeIcons[0] = view.findViewById(R.id.iv_safe1);
        mSafeIcons[1] = view.findViewById(R.id.iv_safe2);
        mSafeIcons[2] = view.findViewById(R.id.iv_safe3);
        mSafeIcons[3] = view.findViewById(R.id.iv_safe4);

        mDesIcons = new ImageView[4];
        mDesIcons[0] = view.findViewById(R.id.iv_des1);
        mDesIcons[1] = view.findViewById(R.id.iv_des2);
        mDesIcons[2] = view.findViewById(R.id.iv_des3);
        mDesIcons[3] = view.findViewById(R.id.iv_des4);

        mSafeDes = new TextView[4];
        mSafeDes[0] = view.findViewById(R.id.tv_des1);
        mSafeDes[1] = view.findViewById(R.id.tv_des2);
        mSafeDes[2] = view.findViewById(R.id.tv_des3);
        mSafeDes[3] = view.findViewById(R.id.tv_des4);

        mSafeDesBar = new LinearLayout[4];
        mSafeDesBar[0]=view.findViewById(R.id.ll_des1);
        mSafeDesBar[1]=view.findViewById(R.id.ll_des2);
        mSafeDesBar[2]=view.findViewById(R.id.ll_des3);
        mSafeDesBar[3]=view.findViewById(R.id.ll_des1);

        //箭头
        ivArrow = view.findViewById(R.id.iv_arrow);
        //上方布局
        rlDesRoot= view.findViewById(R.id.rl_des_root);
        //下方布局
        llDesRoot = view.findViewById(R.id.ll_des_root);
        //给上方布局设置点击事件,控制下方布局的开关
        rlDesRoot.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                toggle(isOpen);
            }
        });
        imageManager = x.image();


        return view;
    }

    //打开或关闭安全描述信息
    private void toggle(boolean isOpen) {
        //属性动画
        ValueAnimator animator = null;
        if (isOpen){
            isOpen = false;
            //关闭
             animator = ValueAnimator.ofInt(mDesHeight, 0);
        }else {
            isOpen = true;
            //开启
            animator = ValueAnimator.ofInt(0, mDesHeight);
        }
        //动画更新的监听
        animator.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {
            @Override
            public void onAnimationUpdate(ValueAnimator animation) {
                //获取最新的高度值
                Integer height = (Integer) animation.getAnimatedValue();
                //重新修改布局高度
                layoutParams.height = height;
                llDesRoot.setLayoutParams(layoutParams);
            }
        });

        final boolean finalIsOpen = isOpen;

        animator.addListener(new Animator.AnimatorListener() {
            @Override
            public void onAnimationStart(Animator animation) {

            }

            @Override
            public void onAnimationEnd(Animator animation) {
                //动画结束,更改箭头方向
                if (finalIsOpen){
                    ivArrow.setImageResource(R.drawable.arrow_up);
                }else {
                    ivArrow.setImageResource(R.drawable.arrow_down);

                }
            }

            @Override
            public void onAnimationCancel(Animator animation) {

            }

            @Override
            public void onAnimationRepeat(Animator animation) {

            }
        });
        animator.setDuration(200);//设置动画时间
        animator.start();//开始执行动画

    }

    @Override
    public void refreshView(AppInfo data) {
        ArrayList<AppInfo.SafeInfo> safeInfos = data.safe;
        for (int i = 0; i < 4; i++) {
            if (i<safeInfos.size()){
                //目前要显示的图片
                AppInfo.SafeInfo info = safeInfos.get(i);
                //安全标识图片
                imageManager.bind(mSafeIcons[i], HttpHelper.URL+"image?name="+info.safeUrl);
                //安全描述文字
                mSafeDes[i].setText(info.safeDes);
                //安全描述标识图片
                imageManager.bind(mDesIcons[i],HttpHelper.URL+"image?name="+info.safeDesUrl);
            }else {
                //剩下不应该显示的图片
                mDesIcons[i].setVisibility(View.GONE);
                //隐藏多余的描述条目
                mSafeDesBar[i].setVisibility(View.GONE);

            }
        }
        //获取安全描述的完整高度
        llDesRoot.measure(0,0);
         mDesHeight = llDesRoot.getMeasuredHeight();
        //修改安全描述布局高度为0,达到隐藏效果
        layoutParams = (LinearLayout.LayoutParams) llDesRoot.getLayoutParams();
        layoutParams.height = 0;
        llDesRoot.setLayoutParams(layoutParams);

    }
}
